﻿using Face8280WebSocketServer.Utility;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Command.Models
{
    /// <summary>
    /// 人员特征数据
    /// </summary>
    public class FeatureDataModel
    {
        /// <summary>
        /// 二进制数据方式上传图片
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        public FeatureDataModel(FeatureType type, byte[] data) : this(type, FeatureDataType.Stream, String.Empty, data, data.Length)
        {

        }
        /// <summary>
        /// Url方式上传图片
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        /// <param name="len"></param>
        public FeatureDataModel(FeatureType type, string data, int len) : this(type, FeatureDataType.Url, data, null, len)
        {

        }

        public FeatureDataModel(FeatureType type, FeatureDataType dataType, string data, byte[] binaryData, int len, int num = 1)
        {
            Data = data;
            Type = type;
            DataType = dataType;
            Len = len;
            Num = num;
            BinaryData = binaryData;
            Md5 = MD5Utility.Encrypt(binaryData);
        }

        public FeatureDataModel()
        {

        }
        private void CheckValue()
        {
            //if (DataType == FeatureDataType.Stream && BinaryData == null)
            //{
            //    ThrowHelper.ArgumentNullException("添加人员，缺少人员图片二进制数据");
            //}
            if (BinaryData != null)
            {
                // var sType = Encoding.UTF8.GetBytes(Type.ToString());
                // DataBytes = sType.Concat(BinaryData).ToArray();
                Md5 = MD5Utility.Encrypt(BinaryData);
            }
        }
        /// <summary>
        /// 文件序号
        /// </summary>
        public int Num { get; set; }
        /// <summary>
        /// 类型 1、人脸特征码:face,2、指纹特征码:fp  + (索引号 1-10);3、人员头像:photo( jpg格式)
        /// </summary>
        [Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
        public FeatureType Type { get; set; }
        /// <summary>
        /// 数据大小
        /// </summary>
        public int Len { get; set; }
        /// <summary>
        /// dataType 有三种格式
        /// Base64，则data字段为base64字符串
        /// Url，则data字段为一个http/https开头的url
        /// Stream，则表示文件已二进制流方式传输，需要等待后续包
        /// </summary>
        [Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
        public FeatureDataType DataType { get; set; }
        /// <summary>
        /// 数据内容
        /// </summary>
        public string Data { get; set; }

        public string Md5 { get; set; }
        [Newtonsoft.Json.JsonIgnore]
        public byte[] BinaryData { get; set; }
        //[Newtonsoft.Json.JsonIgnore]
        //public byte[] DataBytes { get; private set; }


    }
}
